package com.liao.work.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.work.R;
import com.liao.work.domain.InfoList;

public class AdapterInfoListListViewItemAdapter extends BaseAdapter {

    private List<InfoList> infoLists = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public AdapterInfoListListViewItemAdapter(Context context, ArrayList<InfoList> lists) {
        this.context = context;
        this.infoLists = lists;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return infoLists.size();
    }

    @Override
    public InfoList getItem(int position) {
        return infoLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_infolist_listview_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((InfoList) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(InfoList infoList, ViewHolder holder) {
        holder.img.setImageResource(infoList.getImage());
        holder.text1.setText(infoList.getText1());
        holder.text2.setText(infoList.getText2());
    }

    protected class ViewHolder {
        private ImageView img;
        private TextView text1;
        private TextView text2;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            text1 = (TextView) view.findViewById(R.id.text1);
            text2 = (TextView) view.findViewById(R.id.text2);
        }
    }
}
